package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"io"
	"news/cache"
	"news/models"
	"os"
	"path/filepath"
	time2 "time"
)

type NewsController struct {
	beego.Controller
}

func (c *NewsController) Upload() {
	file, m, err := c.GetFile("image")
	if err != nil {
		return
	}

	if m.Size > 1*1024*1024 {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "图片格式只允许上传1MB",
		}
		c.ServeJSON()
		return
	}

	ext := filepath.Ext(m.Filename)
	if ext != ".jpg" && ext != ".png" {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "图片格式不正确",
		}
		c.ServeJSON()
		return
	}

	openFile, err := os.OpenFile("static/img/"+m.Filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
	if err != nil {
		return
	}

	io.Copy(openFile, file)
	c.Data["json"] = Res{
		Code:    200,
		Data:    "http://127.0.0.1:8080/static/img/" + m.Filename,
		Message: "新闻图片上传成功",
	}
	c.ServeJSON()
	return
}

func (c *NewsController) Create() {
	title := c.GetString("title")
	address := c.GetString("address")
	typesId, _ := c.GetInt("typesId")
	author := c.GetString("author")
	image := c.GetString("image")
	time := c.GetString("time")
	pl := c.GetString("pl")
	content := c.GetString("content")

	if title == "" || address == "" || typesId == 0 || author == "" || image == "" || pl == "" || content == "" {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "内容不能为空",
		}
		c.ServeJSON()
		return
	}

	news := models.News{
		Title:   title,
		Address: address,
		TypesId: typesId,
		Author:  author,
		Image:   image,
		Time:    time,
		Pl:      pl,
		Content: content,
	}

	if err := news.GetNewsBy(); err != nil {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "系统错误",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("news_id", news.ID)
	jsonData, _ := json.Marshal(news)
	cache.Rdb.Set(cache.Ctx, key, jsonData, time2.Hour*2)
	news.Create()
	c.Data["json"] = Res{
		Code:    200,
		Data:    news,
		Message: "新闻添加成功",
	}
	c.ServeJSON()
	return
}

func (c *NewsController) List() {

	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")

	keyword := c.GetString("keyword")
	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pageSize")
	var news models.News
	var list []*models.News

	key := fmt.Sprintf("news_list_%s_%d", keyword, page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if cacheData == "" {
		list, _ = news.List(keyword, page, pageSize)
		if len(list) != 0 {
			jsonData, _ := json.Marshal(list)
			cache.Rdb.Set(cache.Ctx, key, jsonData, time2.Hour*2)
		}
	} else {
		fmt.Println("获取成功")
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.Data["json"] = Res{
		Code:    200,
		Data:    list,
		Message: "查询成功",
	}
	c.ServeJSON()
	return
}

func (c *NewsController) Del() {
	id, _ := c.GetInt("id")
	var news models.News
	news.ID = uint(id)

	if err := news.GetNewsBy(); err != nil {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "系统错误",
		}
		c.ServeJSON()
		return
	}

	if news.Status == 1 {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "已发布的不能删除",
		}
		c.ServeJSON()
		return
	}

	if err := news.Del(); err != nil {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "系统错误",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("news_id_%d", news.ID)
	cache.Rdb.Del(cache.Ctx, key)
	c.Data["json"] = Res{
		Code:    200,
		Data:    news,
		Message: "删除成功",
	}
	c.ServeJSON()
	return
}

func (c *NewsController) Detail() {
	id, _ := c.GetInt("id")
	var news models.News
	news.ID = uint(id)

	key := fmt.Sprintf("news_id_%d", news.ID)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if cacheData != "" {
		jsonData, _ := json.Marshal(news)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time2.Hour*2)
	} else {
		fmt.Println("获取成功")
		json.Unmarshal([]byte(cacheData), &news)
	}
	c.Data["json"] = Res{
		Code:    200,
		Data:    news,
		Message: "详情查询成功",
	}
	c.ServeJSON()
	return

}
